<?php

namespace App\Http\Controllers;

use DB;
use Illuminate\Http\Request;
use Validator;

use Ptime3\Functions\JoinData;

class MailBlackController extends Controller
{
    /**
     * @api {post} /mail/black GetMailBlackList
     * @apiVersion 0.2.0
     * @apiName GetMailBlackList
     * @apiGroup mail
     * @apiPermission app
     *
     * @apiDescription 获取被拉黑用户列表
     *
     * @apiParam {Number} user_id *用户id.
     *
     * @apiSuccess {Boolean} error 是否错误.
     * @apiSuccess {Object[]} result 被拉黑用户.
     * @apiSuccess {Number} result.id 用户id.
     * @apiSuccess {String} result.name 用户名.
     * @apiSuccess {String} result.icon 用户头像.
     * @apiSuccess {DateTime} result.created_at 创建时间.
     * @apiSuccess {String} message 提示消息.
     *
     * @apiError paramError 参数错误.
     * @apiSampleRequest http://192.168.1.120:82/api/mail/black
     */
    public function index(Request $request){
        $input = $request->all();
        //参数检验
        $validator = Validator::make($input,
            [
                'user_id'         => 'required|integer|min:1'
            ]
        );
        if($validator->fails()){
            return $this::jsonResponse(true,$validator->messages(),"paramError");
        }

        $userId = $input["user_id"];

        $blackResult = DB::table("ml_black")
            ->select("ml_black.blacked_user_id AS user_id","ml_black.created_at")
            ->where("ml_black.user_id",$userId)
            ->orderBy("created_at","DESC")
            ->get();

        $blackResult = JoinData::user($blackResult);

        return $this->jsonResponse(false,$blackResult);
    }

    /**
     * @api {post} /mail/black CreateMailBlack
     * @apiVersion 0.2.0
     * @apiName CreateMailBlack
     * @apiGroup mail
     * @apiPermission app
     *
     * @apiDescription 拉黑用户
     *
     * @apiParam {Number} user_id *用户id.
     * @apiParam {Number} blacked_user_id *被拉黑用户id.
     *
     * @apiSuccess {Boolean} error 是否错误.
     * @apiSuccess {Number} result 新建赞id.
     * @apiSuccess {String} message 提示消息.
     *
     * @apiError paramError 参数错误.
     * @apiSampleRequest http://192.168.1.120:82/api/mail/black
     */
    public function store(Request $request){
        $input = $request->all();
        //参数检验
        $validator = Validator::make($input,
            [
                'user_id'         => 'required|integer|min:1',
                'blacked_user_id' => 'required|integer|min:1',
            ]
        );
        if($validator->fails()){
            return $this::jsonResponse(true,$validator->messages(),"paramError");
        }

        $userId        = $input["user_id"];
        $blackedUserId = $input["blacked_user_id"];

        $blackData = [
            "user_id"         => $userId,
            "blacked_user_id" => $blackedUserId
        ];

        $blackId = DB::table("ml_black")->insertGetId($blackData);

        return $this->jsonResponse(false,$blackId);
    }

    /**
     * @api {delete} /mail/black DeleteMailBlack
     * @apiVersion 0.2.0
     * @apiName DeleteMailBlack
     * @apiGroup mail
     * @apiPermission app
     *
     * @apiDescription 取消拉黑用户
     *
     * @apiParam {Number} user_id *用户id.
     * @apiParam {Number} blacked_user_id *被拉黑用户id.
     *
     * @apiSuccess {Boolean} error 是否错误.
     * @apiSuccess {Number} result 结果.
     * @apiSuccess {String} message 提示消息.
     *
     * @apiError paramError 参数错误.
     * @apiSampleRequest http://192.168.1.120:82/api/mail/black
     */
    public function destroy(Request $request){
        $input = $request->all();
        //参数检验
        $validator = Validator::make($input,
            [
                'user_id'         => 'required|integer|min:1',
                'blacked_user_id' => 'required|integer|min:1',
            ]
        );
        if($validator->fails()){
            return $this::jsonResponse(true,$validator->messages(),"paramError");
        }

        $userId        = $input["user_id"];
        $blackedUserId = $input["blacked_user_id"];

        $blackResult = DB::table("ml_black")
            ->where("user_id",$userId)
            ->where("blacked_user_id",$blackedUserId)
            ->delete();

        return $this->jsonResponse(false,$blackResult);
    }

}